package cn.itcast.producer;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 发布与订阅消息生产者
 *
 * @author lee.siu.wah
 * @version 1.0
 * <p>File Created at 2020-11-24<p>
 */
@Component("a")
public class FanoutProducer {

    @Resource
    private AmqpTemplate amqpTemplate;
    @Value("${order.fanout}")
    private String exchange;

    // 生产消息方法
    public void saveOrder(Long userId, Long goodsId, Integer buyNum){
        // 定义消息
        String msg = "用户: " + userId + ", 购买了商品: " + goodsId +
                ", 订单编号: " + UUID.randomUUID().toString();
        /**
         * String exchange 交换机
         * String routingKey 路由键
         * Object message 消息内容
         */
        amqpTemplate.convertAndSend(exchange, "", msg);
    }
}
